// USPVSIdeology
log using USPVSIdeology.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\PVScleandata.dta" // Prosser, Magasin, Proulx and Haddock, UK Progressive Values Dataset, April 2024 // Accessed on March 16 2025

// Create a weight
gen weight = 1

* Code variables into categories
recode age (min/24=1 "0-24") (25/34=2 "25-34") (35/44=3 "35-44") (45/54=4 "45-54") (55/max=5 "55+"), generate(age_group)
recode ethnicity (1=1) (2=2) (3=3) (5=4) (6=4) (7=4) (4=5) (8=6), generate(ethnic_group)
label define ethnic_group_lbl 1 "White" 2 "Black" 3 "Hispanic" 4 "Asian or Native Hawaiian/other Pacific Islander, non-Hispanic" 5 "Native American/Alaska Native or other race, non-Hispanic" 6 "Multiple races, non-Hispanic"
label values ethnic_group ethnic_group_lbl
recode education (3=1) (4=1) (5=1) (6=2) (7=2) (8=3) (9=4) (10=4) (11=4), generate(ed_group)
recode householdincome (1=1) (2=1) (3=1) (4=1) (5=1) (6=1) (7=1) (8=2) (9=2) (10=2) (11=2) (12=3) (13=4), generate(inc_group)

* Generate totals for the weights - these are based on the ANES24 pre-election wave as this dataset has political selfid, unlike census data. The below are for 1-4, i.e. very liberal to centrist, on the political selfid scale. This mirrors the PVS sample. 

replace gender=. if inrange(gender, 3, 5)
rename gender FemaleGender
gen sextot=.
replace sextot = 0.4663 if FemaleGender == 1 // Male
replace sextot = 0.5337 if FemaleGender == 2 // Female

gen agetot=.
replace agetot = 0.1227 if age_group == 1 //18-24
replace agetot = 0.1848 if age_group == 2 //25-34
replace agetot = 0.1891 if age_group == 3 //35-44
replace agetot = 0.1544 if age_group == 4 //45-54
replace agetot = 0.3491 if age_group == 5 //55+

gen ethtot=. 
replace ethtot = 0.6038 if ethnic_group == 1 // White
replace ethtot = 0.1186 if ethnic_group == 2 // Black
replace ethtot = 0.1592 if ethnic_group == 3 // Hispanic
replace ethtot = 0.0659 if ethnic_group == 4 // Asian or Native Hawaiian
replace ethtot = 0.0024 if ethnic_group == 5 // Native American
replace ethtot = 0.0502 if ethnic_group == 6 // Multiple

gen edtot=.
replace edtot = 0.4408 if ed_group == 1 // Uptosomecollege
replace edtot = 0.0926 if ed_group == 2 // Trade/assocdegree
replace edtot = 0.2754 if ed_group == 3 // Undergraddegree
replace edtot = 0.1912 if ed_group == 4 // Postgradandabove

gen inctot=.
replace inctot = 0.2528 if inc_group == 1 //under 60k
replace inctot = 0.2084 if inc_group  == 2 //60-100k
replace inctot = 0.1966 if inc_group  == 3 //100-150k
replace inctot = 0.3422 if inc_group  == 4 //over 150k

* Rake the weights using the Stata survwgt package
survwgt rake weight , by(FemaleGender age_group ethnic_group ed_group inc_group) totvars(sextot agetot ethtot edtot inctot) generate(rakedweight)

// Reverse politicalID variable so left coded high
foreach var in lefttoright {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

// Correlations
pwcorr pvs rlefttoright [aweight=rakedweight], sig

// Ideological constraint
*Create graduate dummy
gen Graduate=. 
replace Graduate=0 if inrange(education, 3, 7)
replace Graduate=1 if inrange(education, 8, 11)

// Correlations
pwcorr pvs_md pvs_reireversed pvs_cac pvs_pc [aweight=rakedweight], sig
pwcorr pvs_md pvs_reireversed pvs_cac pvs_pc if Graduate==1 [aweight=rakedweight], sig 
pwcorr pvs_md pvs_reireversed pvs_cac pvs_pc if Rlefttoright>1 [aweight=rakedweight], sig 

log close